package com.example.administrator.liveapp.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.liveapp.ApplicationUtils;
import com.example.administrator.liveapp.R;
import com.example.administrator.liveapp.Xutils3.MyCallBack;
import com.example.administrator.liveapp.Xutils3.XUtils;
import com.example.administrator.liveapp.adapter.MessageRemindAdapter;
import com.example.administrator.liveapp.bean.MessageRemindBean;
import com.example.administrator.liveapp.utils.AppUrl;
import com.example.administrator.liveapp.view.FullyLinearLayoutManager;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * 消息提醒activity
 */

public class MessageRemind extends AppCompatActivity implements View.OnClickListener {

    private RecyclerView remind_recycleView;
    private MessageRemindBean bean;
    private TextView no_content;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message_remind);
        initView();
    }

    private void initView() {
        remind_recycleView = (RecyclerView) findViewById(R.id.remind_recycleView);
        no_content = (TextView) findViewById(R.id.no_content);
        findViewById(R.id.remind_returns).setOnClickListener(this);
        initData();
    }

    private void initData() {
        String url = AppUrl.URL + AppUrl.MessageRemindUrl;
        Map<String, String> map = new HashMap<>();
        if (ApplicationUtils.getInstance().loginBean == null) {
            Toast.makeText(MessageRemind.this, "用户未登录", Toast.LENGTH_SHORT).show();
            startActivity(new Intent(MessageRemind.this,WxLoginPage.class));
            overridePendingTransition(R.anim.zoomin, R.anim.zoomout);
            return;
        }
        map.put("consumerId", ApplicationUtils.getInstance().loginBean.getConsumerid());
        if(ApplicationUtils.getInstance().loginBean.getConsumertel()!=null){
            map.put("consumerTel", ApplicationUtils.getInstance().loginBean.getConsumertel().toString());
        }else if(ApplicationUtils.getInstance().loginBean.getWxopenid()!=null){
            map.put("openid", ApplicationUtils.getInstance().loginBean.getWxopenid().toString());
        }
        map.put("status", "1");
        XUtils.Get(url, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(result);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                Gson gson = new Gson();
                bean = gson.fromJson(result, MessageRemindBean.class);
                String code = jsonObject.optString("code");
                if (code.equals("4818")||code.equals("4825")) {
                    recyclerView();
                } else if (code.equals("4819")||code.equals("4827")) {
                    no_content.setVisibility(View.VISIBLE);
                } else if (code.equals("4821")) {
                    Toast.makeText(MessageRemind.this, "用户未登录", Toast.LENGTH_SHORT).show();
                    startActivity(new Intent(MessageRemind.this,WxLoginPage.class));
                    overridePendingTransition(R.anim.zoomin, R.anim.zoomout);
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
            }

        });
    }

    private void recyclerView() {
        FullyLinearLayoutManager linear = new FullyLinearLayoutManager(this);
        remind_recycleView.setLayoutManager(linear);
        remind_recycleView.setHasFixedSize(true);
        MessageRemindAdapter adapter = new MessageRemindAdapter(bean.getNotify());
        remind_recycleView.setAdapter(adapter);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.remind_returns:
                finish();
                break;
        }
    }
}
